import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import axios from 'axios'
export default new Vuex.Store({
  state: {//117.50.141.71
    base_url: 'http://localhost:8088',
    user: null
  },
  mutations: {
    setUser(state, user) {
      state.user = user
    },
    setUserMoney(state,money){
      state.user.money=money
    }
  },
  actions: {
    getUser(context) {
      let token = localStorage.getItem('token')
      console.log("token:" + token)
      if (token != null) {
        console.log(context)
        if (context.state.user == null) {
            axios.get(context.state.base_url + '/token/?token=' + token).then(data => {
            //console.log(data)
            if (data.data.code == "SUCCESS") {
              context.commit('setUser', data.data.data)
            } else {
              localStorage.removeItem('token')
            }
          })
        }
      }
    }
  },
  modules: {}
})